package com.sxbbc.common.basics.utils.qr;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sxbbc.common.basics.enums.RandNumType;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.files.RestUploadFileInfo;
import com.sxbbc.common.basics.files.UploadUtils;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.radoms.RandNumUtils;

/**
 * 二维码工具类
 *
 * @author arno
 */
public class QRCodeUtil {

    public static QRCodeUtil QRCodeUtil = null;

    public static QRCodeUtil getInstance() {
        if (QRCodeUtil == null) {
            synchronized (QRCodeUtil.class) {
                if (QRCodeUtil == null) {
                    QRCodeUtil = new QRCodeUtil();
                }
            }
        }
        return QRCodeUtil;
    }

    public final String CHARSET = "utf-8";
    public final String FORMAT_NAME = "png";
    /**
     * 二维码尺寸
     */
    private final int QRCODE_SIZE = 200;

    /**
     * 生成二维码
     */
    private BufferedImage createImage(String content) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
                hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    /**
     * 生成二维码
     *
     * @param content 文本内容
     * @throws Exception
     */
    public InputStream encode(String content) {
        InputStream is = null;
        try {
            BufferedImage image = QRCodeUtil.createImage(content);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(image, FORMAT_NAME, os);
            is = new ByteArrayInputStream(os.toByteArray());
        } catch (Exception e) {
            throw new ThrowJsonException("创建二维码失败");
        }
        return is;
    }

    public static void main(String[] args) throws Exception {
        InputStream is = QRCodeUtil.getInstance().encode("http://www.sanxininfo.cn/");
        String filename = DateUtil.getInstance().currentTimeMillis() + RandNumUtils.getInstance().get(RandNumType.NUMBER, 5) + "." + QRCodeUtil.getInstance().FORMAT_NAME;
        RestUploadFileInfo r = UploadUtils.getInstance().uploadFile(is, filename);
        System.out.println(r.getData());
    }
}
